//
//  AppDelegate.swift
//  微博
//
//  Created by 康智桐 on 2018/3/27.
//  Copyright © 2018年 Kk. All rights reserved.
//

import UIKit
import UserNotifications
import SVProgressHUD
import AFNetworking


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        
        setupAdditions()
        
        
       
        
        
        
        
        // Override point for customization after application launch.
        //sleep(2)
        window = UIWindow()
        window?.backgroundColor = UIColor.white
        window?.rootViewController = WBMainViewController() // 设置界面的根视图为WBMainViewController
        window?.makeKeyAndVisible()
        
        loadAppInfo()
        
        return true
    }

}
//设置应用程序额外信息

extension AppDelegate {
    
    func setupAdditions(){
        //设置SVProgressHUD最小解除时间
        SVProgressHUD.setMinimumDismissTimeInterval(1)
        
        //设置网络加载指示器
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        
        //#available用来检测设备版本
        if #available(iOS 10.0, *) {
            
            //ios 10.0以上  执行以下授权方法
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.carPlay,.sound]){ (success,error) in
                print("授权" + (success ? "成功" : "失败"))
                
            }
        } else {
            // Fallback on earlier versions
            // ios 10.0 以下 取得用户授权显示通知[通知条/声音/bageNumber]
            let notifySettings = UIUserNotificationSettings(types: [.alert,.badge,.sound], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(notifySettings)
            
            
        }
        
    }
    
}



extension AppDelegate {
    
    func loadAppInfo(){
        //模拟异步
        DispatchQueue.global().async {
            let url = Bundle.main.url(forResource: "main.json", withExtension: nil)
            
            let data = NSData(contentsOf: url!)
            
            let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let jsonPath = (docDir as NSString).appendingPathComponent("main.json")
            //直接保存在沙盒，等待下一次程序启动调用
            data?.write(toFile: jsonPath, atomically: true)
            
            print("应用加载完毕\(jsonPath)")
            
        }
        
    }
    
}
